﻿/*----------------------------------------------------------------
// Copyright (C) 2011 周士祎
// 版权所有。 
//
// 文件名：DataAccessLogging
// 文件功能描述：用户登录数据层
//
// 
// 创建标识：2012-01-03 周士祎
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DbAdapter.sqldbAdapter;

namespace DAO.WaiGaoQiao
{
    public class DataAccessLogging
    {
        public string Da_Getlogpassword(string logname)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                conn.Open();
                string query = "select LogPassWord from Tbt_LogUser where LogName = '" + logname + "'";
                sqldb sqldb = new sqldb();
                object logpass = sqldb.ExecuteScalar(conn, CommandType.Text, query);
                return (logpass == null) ? "" : logpass.ToString();
            }
        }
        /// <summary>
        /// 保存或更新
        /// </summary>
        /// <param name="paraArray"></param>
        /// <returns></returns>
        public int Save(object[] paraArray)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                return db.ExecuteNonQuery(conn, "Proc_LogUser", paraArray);
            }
        }

        /// <summary>
        /// 用户信息分页
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="startindex"></param>
        /// <returns></returns>
        public DataSet GetLogList(int pagesize, int startindex)
        {
            using(SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex-1);
                int endrow = pagesize * startindex+1;
                string query = "select B.* from (select ROW_NUMBER() OVER(ORDER BY A.LogDt DESC) rownumber,"
                             + "A.LogName,A.LogPassword,A.PasswordConfirm,ISNULL(A.PasswordHint,'') PasswordHint,A.LogDt,A.IsLocked,"
                             + "isnull(A.LockDt,'1900-01-01') LockDt,D.RoleName from Tbt_LogUser A left join Tbt_LogUserAndRole C on A.LogName=C.LogName "
                             +" left join Tbt_Roles D on C.RoleId=D.RoleId ) B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }            
        }
        /// <summary>
        /// 获取登录用户的数量
        /// </summary>
        /// <returns></returns>
        public int GetLogCount()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select count(1) from Tbt_LogUser";
                object rowcount = db.ExecuteScalar(conn, CommandType.Text, query);
                return Convert.ToInt32(rowcount);
            }
        }
        /// <summary>
        /// 删除登录用户
        /// </summary>
        /// <param name="logid"></param>
        /// <returns></returns>
        public int DeleteLogUser(string logid)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "delete from Tbt_LogUser where LogName = '" + logid + "'";
                return db.ExecuteNonQuery(conn, CommandType.Text, query);
            }
        }
        /// <summary>
        /// 绑定用户到角色
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int BindUserToRole(string logid, string roleid)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "if Not Exists(select * from Tbt_LogUserAndRole where LogName = '" + logid + "' "
                            + "and RoleId = '" + roleid + "') insert into Tbt_LogUserAndRole(LogName,RoleId) Values('"
                            + logid + "','" + roleid + "')";
                return db.ExecuteNonQuery(conn, CommandType.Text, query);
            }
        }
        /// <summary>
        /// 删除绑定
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int NotBindUserToRole(string logid, string roleid)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "delete from Tbt_LogUserAndRole where LogName = '" + logid + "' "
                             + "and RoleId = '" + roleid + "'";
                return db.ExecuteNonQuery(conn, CommandType.Text, query);
            }
        }
    }
}
